home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13542 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  106 lines

  1. Path: news.xnet.com!news-admin
  2. From: Gary Jenkins <grj@jenkins.xnet.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Output to file vs. cout
  5. Date: Mon, 25 Mar 1996 20:04:22 -0600
  6. Organization: XNet - A Full Service Internet Provider - (708) 983-6064
  7. Message-ID: <315750A6.357E152F@jenkins.xnet.com>
  8. References: <JL.96Mar7165152@thyme.id.dth.dk> <4i18mg$ba5@hpbblb.bbn.hp.com> <4impgh$jnl@is-news.gov.ab.ca> <4in7d5$9el@thales.nmia.com>
  9. NNTP-Posting-Host: jenkins.xnet.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=iso-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.0 i486)
  14.  
  15. Gilbert Healton wrote:
  16. > Ron Orr (orr@lisd.env.gov.ab.ca) wrote:
  17. > : Matthias Dittrich (matti) wrote:
  18. > : : jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
  19. > : : >
  20. > : : >Here's another of the probs that comes from porting C to C++:
  21. > : : >
  22. > : : >- I want to be able to redirect program output to either a file or cout.
  23. > : : >  Typically this would be selected by a commandline option ("-o outfile").
  24. > : : >
  25. > : : >- In C I would do something like this:
  26. > : : >
  27. > : : >
  28. > : : >    main()
  29. > : : >    {
  30. > : : >      FILE *outputfile;
  31. > : : >
  32. > : : >      if (commandline == do_output_to_file)
  33. > : : >         outputfile = stdout;
  34. > : : >      else
  35. > : : >         outputfile = fopen(passed_filename, "w");
  36. > : : >
  37. > : : >      fprintf(outfile, "Hello world\n");
  38. > : : >
  39. > : : >      fclose(outfile);  /* Maybe - not allways necassary */
  40. > : : >    }
  41. > : : >
  42. > : : >- The question is:  How is this done in C++ ?
  43. > : : >
  44. > : : >.......
  45. > : : ofstream outfile(passed_filename); /* this constructor opens your file */
  46. > : : outfile << "Hello world" << endl;  /* writes the string */
  47. > :
  48. > : : The destructor closes the file and of course you should do some error checking.
  49. > :
  50. > I, too, am looking for a solution to this. It seems it is taking me longer
  51. > to get all the ins and outs of streams than the C++ language..
  52. > The ability to have a stream that you open, and close/reopen, at various points
  53. > of the file is also of interest.
  54. > --
  55. > ----- Computer Consulting / Web Pages -----    http://www.nmia.com/~ghealton/
  56. > These opinions are my own. Life is learning and I may retract, modify,
  57. > even attack, my previous ideas at any time without notice.
  58.  
  59.  
  60. How about this:
  61.  
  62. #include <fstream.h>
  63. #include <string.h>     // strcmp()
  64. #include <stdlib.h>     // exit()
  65.  
  66. //
  67. // syntax:
  68. //      progname [-o outfile]
  69. //  
  70. int main(int argc, char *argv[])
  71. {   
  72.     // output to stdout as default
  73.     ostream *outputStream = &cout;
  74.  
  75.     //
  76.     // see if output is to a file
  77.     //
  78.     if (strcmp(argv[1],"-o") == 0)
  79.     {
  80.         static ofstream outFileStream(argv[2], ios::out);
  81.  
  82.         if (!outFileStream)
  83.         {
  84.             cerr << "Can't open file" << endl;
  85.             exit (1);
  86.         }
  87.  
  88.         outputStream = &outFileStream;
  89.     }
  90.  
  91.     *outputStream << "Hello World" << endl;
  92.  
  93.     return 0;
  94. }
  95.  
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. Gary Jenkins                    "A noble spirit embiggens the smallest man"
  98. grj@jenkins.xnet.com                                    - Jebediah Springfield
  99. grj@tellabs.com
  100.  
  101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  102.